//
//  HMStatusViewMoDEL.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/12.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {

    // 单例
    static let shareInstance: HMStatusViewModel = HMStatusViewModel()
    
    /// 加载微博数据
    func loadStatus(loadStatusCallback: (statuse: [HMStatus]?, error: NSError?) -> ()) {
        // 1. urlString
        let urlString =  "https://api.weibo.com/2/statuses/home_timeline.json"
        // 2. 参数
        /// [String: String?]: key是String类型, value是String或nil. 原因是数组或字典红的元素不能为nil value一定不能为nil 
        let parameters = ["access_token":HMUserAccountViewModel.sharedInstance.userAccount!.access_token!]
      
        
        HMNetwork.shareInstance.GET(urlString, parameters: parameters, progress: nil, success: { (task, responseObject) -> Void in
//            HMPrint("获取微博数据成功了: \(responseObject)")
            
            
            // 获取微博数据
            if let dict = responseObject as? [String: AnyObject], let statusesDict = dict["statuses"]as? [[String: AnyObject]] {
                // 定义数组来保存转好的微博模型
                var statusModels: [HMStatus] = []
                
                // 遍历每个字典
                for dict in statusesDict {
                    // 字典转模型
                    let status = HMStatus(dict: dict)
                    
                    // 将模型添加到数组里面
                    statusModels.append(status)
                }
                loadStatusCallback(statuse: statusModels, error: nil)
            }
            }) { (task, error) -> Void in
                loadStatusCallback(statuse: nil, error: error)

        }
        
    }
    
}

